﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YTSK
{
    class ServerConfig
    {
        public static string ServerAddress;
        public static string UserName;
        public static string PassWord;
        private static string _token;
        // public static DateTime Version = new DateTime(2021,8,18);
        public static DateTime Version = new DateTime(2021,8,23);
        // public static DateTime Version = new DateTime(2021,7,14);
        public static Company _company = Company.广州Iteng  ;
        // public static Company _company = Company.上海Iteng;
        // public static Company _company = Company.深圳Iteng;

        public static string GetToken()
        {
            return _token.ObjToString();
        }

        public static void settoken(string token)
        {
            _token = token;
        }

        #region 获取登录的token--覃豪@20170225
        /// <summary>
        /// 获取登录的token
        /// </summary>
        /// <param name="cAddress">服务器地址</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        public static string Login()
        {
            #region 获取登录的token
            var err = "";
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("UserName", UserName);// "printControl"
            dic.Add("Password", PassWord);// "cSysAABBCCaaa"  
            dic.Add("YTSKVer", ServerConfig.Version);
            var url = ServerAddress + "/api/oms/SysCommon/LoginVer";
            string s = Common.handleHttp.Post(dic, url);
            var d1 = Common.handleJSON.Deserialize(s);
            var suc = d1.getValue("result").ObjToInt();
            if (suc == -5) 
            {
                err += "版本已经升级，请获取新版后重新登陆。";
                url = $"{ServerAddress}/税控接口.exe";
                System.Diagnostics.Process.Start(url);
                return err;
            }
            if (suc == -1)
            {
                err = d1.getValue("resultdetail").ObjToString();
                return err;
            }
            var token = d1.getValue("token").ObjToString();
            settoken(token);
            return err;
            #endregion
        }

        public static void ReLogin()
        {
            if (GetToken().Length == 0)
            {
                var f = new FrmKP.FrmLogin();
                f.ShowDialog();
            }
        }
        #endregion

    }

    public enum Company
    {
        广州Iteng,
        上海Iteng,
        深圳Iteng
    }

}
